%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function [new_im,im,d_cen] = c_centroid(id1,id2, region)
global mov
sil1 = mov(id1).segmented;
sil2 = mov(id2).segmented;

new_im = zeros([size(sil1) 3]);


A = bwmorph(sil1, 'remove');
A = bwmorph( A , 'dilate');
A(:,:,2) = A;

new_im(A == 1) = 255;


im = new_im(region(3):region(4),region(1):region(2),:);
imshow(im)

B = bwmorph(sil2, 'remove');
B = bwmorph( B , 'dilate');

index = zeros(size(new_im));

index(:,:,2) = (B == 1);


new_im( logical( index )) = 255;



new_im = new_im(region(3):region(4),region(1):region(2),:);

figure;imshow(new_im)
hold on

p1 = [(mov(id1).features{4} - region(3) + 1) (mov(id1).features{3} - region(1) + 1)];
p2 = [(mov(id2).features{4} - region(3) + 1) (mov(id2).features{3} - region(1) + 1)];

new_im = plot([p1(2),p2(2)],[p1(1),p2(1)],'Color','w','LineWidth',2)

d_cen = pdist([p1; p2])
